package com.hanyao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyao.reggie.bean.Category;
import com.hanyao.reggie.bean.Dish;
import com.hanyao.reggie.bean.DishFlavor;
import com.hanyao.reggie.common.R;
import com.hanyao.reggie.dto.DishDto;
import com.hanyao.reggie.service.CategoryService;
import com.hanyao.reggie.service.DishFlavorService;
import com.hanyao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author 寒钥
 * @version 1.0
 */
@RequestMapping("/dish")
@RestController
@Slf4j
public class DishController {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        String key = "dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //创建分页器
        Page<Dish> dishPage = new Page<>(page,pageSize);

        Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
        //增加查询条件
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(name != null, Dish::getName, name);
        lambdaQueryWrapper.orderByDesc(Dish::getCreateTime);
        //分页查询数据库
        dishService.page(dishPage,lambdaQueryWrapper);
        //拷贝除分页内容之外的信息到disDtoPage对象
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //将分页内容一次拷贝到dishDtoPage中
        List<Dish> records = dishPage.getRecords();
        List<DishDto> collect = records.stream().map(item -> {
            DishDto dishDto = new DishDto();
            Long categoryId = item.getCategoryId();
            BeanUtils.copyProperties(item, dishDto);
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(collect);
        return R.success(dishDtoPage);
    }
    @GetMapping("/{id}")
    public R<DishDto> show(@PathVariable("id") Long id){
        DishDto dishDto = dishService.getWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        //Set<String> keys = redisTemplate.keys("dish_*");
        String key = "dish_"+dishDto.getCategoryId()+"_1";
        log.info("categoryid={}",dishDto.getCategoryId());
        redisTemplate.delete(key);
        return R.success("修改成功");
    }
    @RequestMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtoList = null;
        String key = "dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if(dishDtoList != null){
            return R.success(dishDtoList);
        }
        //lambda查询
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //查询餐品类别id
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //按更新时间排序
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        //查询正在销售的
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        dishDtoList = new ArrayList<>();
        dishDtoList = list.stream().map(item->{
            DishDto dishDto = new DishDto();
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = Wrappers.lambdaQuery();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
        return R.success(dishDtoList);
    }
}
